/***************************************************************************************************/
/*!
 @header ConvertMovieToAudioAction
 @abstract   ￼Extract audio from QTMovie Automator Action
 @discussion ￼
 An automator action to export audio from QuickTime Movie files.
 
 Created by Hiroki Omae on 09/06/07.
 Copyright 2009 (c) Monkey Coders.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
/***************************************************************************************************/

//-----------------------------------------------------------------------------------------------------------------------------
// File Dependencies
//-----------------------------------------------------------------------------------------------------------------------------
#import "ConvertMovieToAudioAction.h"
#import "MovieToAudioExtractor.h"

//-----------------------------------------------------------------------------------------------------------------------------
// Defines.
//-----------------------------------------------------------------------------------------------------------------------------
#define kTMP_PATH_KEY		@"strAudioSaveDir"
#define kIS_AUTO_DELETE	@"isDeleteSrcFileAfterOp"

//-----------------------------------------------------------------------------------------------------------------------------
// ConvertMovieToAction Impl.
//-----------------------------------------------------------------------------------------------------------------------------
@implementation ConvertMovieToAudioAction


/**
 @brief Automator アクションを実行
 @param [in] input 		Automatorからの入力
 @param [in] anAction 	inputを生成したアクション
 @param [in] error		実行したアクションで発生したエラー情報
 
 @return 入力ファイルから生成したオーディオファイルのパスの配列
 @par [エラー処理]
 変換に失敗したファイルはエラーとして報告されます。どうやったらいいのかは
 よくわかりません。 
 */
- (id)runWithInput:(id)input fromAction:(AMAction *)anAction error:(NSDictionary **)errorInfo {
	
	NSMutableArray *pOutput 		= [[NSMutableArray alloc] init];
	
	// オーディオファイル保存先
	NSString* strAudioSaveDir = [[self parameters] valueForKey:kTMP_PATH_KEY];
	// ソースファイルを処理後に削除
	BOOL	isDeleteSrcFileAfterOp = [[[self parameters] valueForKey:kIS_AUTO_DELETE] boolValue];
	
	// strAudioSaveDir が有効である事を確認
	NSFileManager *pMgr = [NSFileManager defaultManager];
	BOOL isDirectory;
	if ( NO == [pMgr fileExistsAtPath:strAudioSaveDir isDirectory:&isDirectory] || NO == isDirectory ) {
		
		NSString* strMsgFmt =  [[self bundle] localizedStringForKey:@"ERROR_MSG_NO_TMPPATH" value:@"no folder at:%@" table:nil];
		*errorInfo = [NSDictionary dictionaryWithObjectsAndKeys:
					  [NSNumber numberWithInt:fnfErr], 				@"OSAScriptErrorNumber",
					  [NSString stringWithFormat:strMsgFmt, strAudioSaveDir], 	@"OSAScriptErrorMessage", 
					  nil];
		[pOutput release];
		return nil;
	}
	
	// オーディオの展開
	MovieToAudioExtractor *pExtractor = [[MovieToAudioExtractor alloc] init];
	
	for( NSString* inputPath in input ) {
		SEL extractor = @selector(extractAudioFromDictionary:);
		NSString* pathToSave = [[[NSString alloc] initWithString:[strAudioSaveDir stringByExpandingTildeInPath]] autorelease];
		NSDictionary* pArg = [NSDictionary dictionaryWithObjectsAndKeys:
							  [inputPath stringByExpandingTildeInPath], @"path", 
							  pathToSave, @"dir", 
							  [NSNumber numberWithBool:isDeleteSrcFileAfterOp], @"deleteSrc",
							  nil];
		
		// QT の処理はメインスレッドで実行する必要があるので、performSelectorOnMainThreadを使う
		[pExtractor performSelectorOnMainThread:extractor withObject:pArg waitUntilDone:YES];
		
		NSString* pOutPath = [pExtractor outputFilePath];
		if(pOutPath) {
			[pOutput addObject:pOutPath];
		} else {
			NSError* pErr = [pExtractor error];
			if( pErr ) {
				NSString* strMsgFmt =  [[self bundle] localizedStringForKey:@"ERROR_MSG_FOR_PATH" value:@"file:%@, error:%@" table:nil];
				NSString* errMsg = [pErr localizedDescription];
				  *errorInfo = [NSDictionary dictionaryWithObjectsAndKeys:
								 [NSNumber numberWithInt:[pErr code]], 			@"OSAScriptErrorNumber",
								 [NSString stringWithFormat:strMsgFmt, inputPath, errMsg], 	@"OSAScriptErrorMessage", 
								 nil];				
				[pOutput release];
				return nil;
			}
			break;
		}
	}
	
	[pExtractor release];
	
	return pOutput;
}

@end
